package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-07-19 10:06:16
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    private TbBrandMapper tbBrandMapper;
    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbBrand::getBrandName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(TbBrand::getCreateTime);
        //List<TbBrand> tbBrands = tbBrandMapper.selectList(wrapper);
        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);
        return R.OK(tbBrandPage);
    }

    @Override
    public R updateStatusById(TbBrand tbBrand) {
        tbBrandMapper.updateStatusById(tbBrand);
        return R.OK();
    }

    @Override
    public R addBrand(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectOne(new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName()));
        if(brand!=null){
            return R.ERROR(500,"该品牌已存在");
        }
        tbBrand.setBrandUse(10);
        tbBrand.setCreateBy("张三");
        tbBrand.setCreateTime(new Date());
        tbBrandMapper.insert(tbBrand);
        return R.OK();
    }

    @Override
    public R getBrandData() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);
        return R.OK(tbBrands);
    }

    @Override
    public R updateBrand(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if(brand==null){
            return R.ERROR(500,"该品牌不存在");
        }
        tbBrandMapper.updateById(tbBrand);
        return R.OK();
    }

    @Override
    public R deleteBrand(Integer brandId) {
        TbBrand brand = tbBrandMapper.selectById(brandId);
        if(brand==null){
            return R.ERROR(500,"该品牌不存在");
        }
        tbBrandMapper.deleteById(brandId);
        return R.OK();
    }

}




